package com.sky.controller.user;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/user/addressBook")
@Api(tags = "C端地址簿接口")
@RequiredArgsConstructor
public class AddressBookController {

    private final AddressBookService addressBookService;

    /**
     * 查询当前登录用户的所有地址信息
     *
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("查询当前登录用户的所有地址信息")
    public Result<List<AddressBook>> listAddress() {
//        AddressBook addressBook = new AddressBook();
//        addressBook.setUserId(BaseContext.getCurrentId());
        Long userId = BaseContext.getCurrentId();
        List<AddressBook> addressBooks = addressBookService.lambdaQuery().eq(AddressBook::getUserId, userId).list();

//        List<AddressBook> list = addressBookService.list(addressBook);
        return Result.success(addressBooks);
    }

    /**
     * 新增地址
     *
     * @param addressBook
     * @return
     */
    @PostMapping
    @ApiOperation("新增地址")
    public Result saveAddress(@RequestBody AddressBook addressBook) {
        Long userId = BaseContext.getCurrentId();
        addressBook.setUserId(userId);
        addressBookService.save(addressBook);
        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询地址")
    public Result<AddressBook> getAddressById(@PathVariable Long id) {
        AddressBook addressBook = addressBookService.getById(id);
        return Result.success(addressBook);
    }

    /**
     * 根据id修改地址
     *
     * @param addressBook
     * @return
     */
    @PutMapping
    @ApiOperation("根据id修改地址")
    public Result updateAddress(@RequestBody AddressBook addressBook) {
        addressBookService.updateById(addressBook);
        return Result.success();
    }

    /**
     * 设置默认地址
     *
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    @ApiOperation("设置默认地址")
    public Result setDefault(@RequestBody AddressBook addressBook) {
//        addressBookService.setDefault(addressBook);
        AddressBook addressBook1 = addressBookService.lambdaQuery().eq(AddressBook::getIsDefault, 1).one();

        //更改之前的默认地址为普通地址
        addressBook1.setIsDefault(0);
        addressBookService.updateById(addressBook1);

        //更改新的默认地址
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return Result.success();
    }

    /**
     * 根据id删除地址
     *
     * @param id
     * @return
     */
    @DeleteMapping
    @ApiOperation("根据id删除地址")
    public Result deleteAddressById(Long id) {
        addressBookService.removeById(id);
        return Result.success();
    }

    /**
     * 查询默认地址
     */
    @GetMapping("default")
    @ApiOperation("查询默认地址")
    public Result<AddressBook> getDefault() {
        //SQL:select * from address_book where user_id = ? and is_default = 1
        Long userId = BaseContext.getCurrentId();
        AddressBook addressBook = addressBookService.lambdaQuery()
                .eq(AddressBook::getUserId, userId)
                .eq(AddressBook::getIsDefault, 1).one();

        if(addressBook != null){
            return Result.success(addressBook);
        }
//        AddressBook addressBook = new AddressBook();
//        addressBook.setIsDefault(1);
//        addressBook.setUserId(BaseContext.getCurrentId());
//        List<AddressBook> list = addressBookService.list(addressBook);
//
//        if (list != null && list.size() == 1) {
//            return Result.success(list.get(0));
//        }

        return Result.error("没有查询到默认地址");
    }

}
